package cn.ffcs.bpmpvc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.echoice.hibernate.HibernateEntityDao;
import org.echoice.paper.PageBean;
import org.springframework.jdbc.core.RowMapper;

import cn.ffcs.bpmpvc.dao.AppSmsDao;
import cn.ffcs.bpmpvc.model.AppSms;
import cn.ffcs.bpmpvc.web.forms.FlowTaskForm;

public class AppSmsDaoImpl extends HibernateEntityDao<AppSms> implements AppSmsDao{
	public AppSms getByProcinstId(String procinstId){
		String hql="select t from AppSms t where t.procinstId=?";
		List list=getHibernateTemplate().find(hql, procinstId);
		if(list!=null&&list.size()>0){
			return (AppSms)list.get(0);
		}
		return null;
	}
	
	public PageBean queryPageSmsList(FlowTaskForm flowTaskForm,int pageNo, int pageSize) {
		List paramList = new ArrayList();
		StringBuilder sql=new StringBuilder(); 
		sql.append("select t1.id,t1.req_code,t1.req_title,t1.req_type,t1.apply_user,t2.name apply_user_name,t1.apply_dept,t3.name apply_dept_name,t1.procinst_id,t1.op_time");
		sql.append(" from t_app_sms t1,t_organ_person t2,t_organ_organization t3");
		sql.append(" where t1.apply_user = t2.id and t1.apply_dept = t3.id");
		if(flowTaskForm!=null){
			if(StringUtils.isNotBlank(flowTaskForm.getReqCode())){
				sql.append(" and t1.req_code=?");
				paramList.add(flowTaskForm.getReqCode());
			}
			if(StringUtils.isNotBlank(flowTaskForm.getReqTitle())){
				sql.append(" and t1.req_title=?");
				paramList.add(flowTaskForm.getReqTitle());
			}
			
			if(StringUtils.isNotBlank(flowTaskForm.getUserId())){
				sql.append(" and t1.apply_user=?");
				paramList.add(flowTaskForm.getUserId());
			}
		}
		sql.append(" order by t1.id desc");
		PageBean pageBean=super.pagedQuerySQLJdbcTemplate(sql.toString(), new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				AppSms obj=new AppSms();
				obj.setId(rs.getInt("id"));
				obj.setReqCode(rs.getString("req_code"));
				obj.setReqTitle(rs.getString("req_title"));
				obj.setReqType(rs.getString("req_type"));
				obj.setApplyUser(rs.getString("apply_user"));
				obj.setApplyUserName(rs.getString("apply_user_name"));
				obj.setApplyDept(rs.getString("apply_dept"));
				obj.setApplyDeptName(rs.getString("apply_dept_name"));
				obj.setProcinstId(rs.getString("procinst_id"));
				obj.setOpTime(rs.getTimestamp("op_time"));
				return obj;
			}}, pageNo, pageSize, paramList.toArray());
		return pageBean;
	}
}
